package cn.devices.mapper;

import cn.devices.TbDmDto;
import cn.devices.entity.TbDm;
import cn.devices.entity.TbType;
import cn.devices.model.TbDmModel;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>a
 * 部门表 Mapper 接口
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@Repository
public interface TbDmMapper {

    @Insert("insert into tb_dm(dep_code, dep_name, disabled) value (#{depCode},#{depName},0)")
    int tbDmAdd(TbDm tbDm);

    @Update("update tb_dm set disabled=1 where dep_code=#{depCode}")
    int tbDmDeleteFalse(String depCode);

    @Delete("delete from tb_dm where dep_code=#{depCode}")
    int tbDmDeleteTrue(String depCode);

    @Update("update tb_dm set dep_code=#{depCode},dep_name=#{depName} " +
            "where dep_code=#{depCodeOriginal} and disabled=0")
    int tbDmUpdate(TbDmDto dto);

    @Select("select COUNT(*) from tb_dm where dep_code!=#{depCodeOriginal} and dep_name =#{depName} and disabled=0")
    int countDmName(String depCodeOriginal, String depName);

    @Select("<script>" +
            "select count(*) from tb_dm where disabled=0 " +
            "<if test=' (depCode != null and depCode!=\"\") or " +
            " (depName != null and depName!=\"\") '>" +
            "and (" +
            "</if>" +
            "<if test='depCode != null and depCode!=\"\"'>" +
            "   dep_code=#{depCode} " +
            "</if> " +
            "<choose> " +
            "<when  test='(depCode != null and depCode!=\"\") and (depName != null and depName!=\"\")'>" +
            "or dep_name=#{depName} " +
            "</when> " +
            "<when  test='(depCode == null or depCode==\"\") and (depName != null and depName!=\"\")'>" +
            " dep_name=#{depName} " +
            "</when> " +
            "</choose>" +
            ")" +
            "</script>")
    int count(String depCode, String depName);


    @Select("select * from tb_dm where disabled=0")
    List<TbDm> tbDmList();

    @Select("select td.*,tad.type_id,tt.type_name,tt.type_level from tb_dm td left join tb_auth_dm tad on td.dep_code = tad.dep_code\n" +
            "left join tb_type tt on tad.type_id = tt.type_id")
    List<TbDmModel> tbDmModel();

    @Select("select * from tb_type order by type_id")
    List<TbType> tbTypeList();

    @Select("select dep_code, dep_name from tb_dm")
    List<TbDmModel> list();

    @Select("select * from tb_dm where dep_code=#{depCode}")
    TbDm tbDmSelect(String depCode);





}
